package controller

import (
	"fmt"
	htbsc "gitee.com/hangzhou_huitian/ht-basic/basic"
	htutil "gitee.com/hangzhou_huitian/ht-basic/utils"
	"github.com/gin-gonic/gin"
	"hisim/global"
	"net/http"
)

type BasicController struct{}

func (cc *BasicController) RegisterGroup(rg *gin.RouterGroup) {
	rg.GET("area/init", cc.initArea)
}

type Response struct {
	Status int         `json:"status"`
	Text   string      `json:"text"`
	Result interface{} `json:"result"`
}

func (cc *BasicController) initArea(c *gin.Context) {
	areas, err1 := htbsc.SelectAreasBy(nil)
	if err1 != nil {
		htutil.ErrServerInternal(c, err1)
		return
	}

	for _, area := range areas {
		area.ParamAt = htbsc.NowHtTime()
	}

	cf := global.Conf
	var emp Response
	if err := global.HttpRequest[Response](cf.CollectorAddr+"/basic/area/init", http.MethodPost, nil, areas, &emp); err != nil {
		fmt.Println(err.Error())
		return
	} else {
		if emp.Status != 200 {
			fmt.Println("initArea error:", emp.Text)
			htutil.ErrMsgServerInternal(c, emp.Text)
			return
		} else if err = htbsc.Exec("update hisim.patient set visit_no=''"); err != nil {
			fmt.Println("reset patient error:", err.Error())
			htutil.ErrServerInternal(c, err)
			return
		} else if err = htbsc.Exec("truncate table hisim.order_scene"); err != nil {
			fmt.Println("reset patient error:", err.Error())
			htutil.ErrServerInternal(c, err)
			return
		}
	}
	htutil.OKay(c)
}
